home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP12.TXT < prev    next >
Text File  |  1987-11-21  |  27KB  |  587 lines

  1.  
  2.                       Chapter 12 - Dynamic Allocation
  3.  
  4.  
  5.                         WHAT IS DYNAMIC ALLOCATION?
  6.  
  7.              Dynamic allocation is very intimidating to a person the
  8.         first time he comes across it, but that need not be.  Simply
  9.         relax  and  read this chapter carefully and you will have  a
  10.         good grounding in a very valuable programming resource.  All
  11.         of the variables in every program up to this point have been
  12.         static  variables as far as we  are  concerned.   (Actually,
  13.         some  of  them  have been "automatic" and  were  dynamically
  14.         allocated for you by the system,  but it was transparent  to
  15.         you.)   In  this  chapter,  we will study  some  dynamically
  16.         allocated  variables.  They are variables that do not  exist
  17.         when  the program is loaded, but are created dynamically  as
  18.         they are needed.  It is possible, using these techniques, to
  19.         create as many variables as needed, use them, and deallocate
  20.         their space for use by other variables.  As usual, the  best
  21.         teacher is an example, so load and display the program named
  22.         DYNLIST.C.
  23.  
  24.              We  begin by defining a named structure "animal" with a
  25.         few  fields  pertaining  to dogs.   We  do  not  define  any
  26.         variables of this type,  only three pointers.  If you search
  27.         through  the  remainder  of the program,  you will  find  no
  28.         variables defined so we have nothing to store data in.   All
  29.         we have to work with are three pointers, each of which point
  30.         to the defined structure.   In order to do anything, we need
  31.         some variables, so we will create some dynamically.
  32.  
  33.                          DYNAMIC VARIABLE CREATION
  34.  
  35.              The first program statement, which assigns something to
  36.         the   pointer  "pet1"  will  create  a   dynamic   structure
  37.         containing  three variables.   The heart of the statement is
  38.         the "malloc" function buried in the middle of the statement.
  39.         This  is a "memory allocate" function that needs  the  other
  40.         things to completely define it.   The "malloc" function,  by
  41.         default, will allocate a piece of memory on a "heap" that is
  42.         "n" characters in length and will be of type character.  The
  43.         "n"  must be specified as the only argument to the function.
  44.         We will discuss "n" shortly,  but first we need to define  a
  45.         "heap".
  46.  
  47.                               WHAT IS A HEAP?
  48.  
  49.              Every compiler has a set of limitations on it as to how
  50.         big  the executable file can be,  how many variables can  be
  51.         used,  how long the source file can be, etc.  One limitation
  52.         placed  on users by most C compilers is a limit of  64K  for
  53.         the executable code if you happen to be in the small  memory
  54.         model.   This  is because the IBM-PC uses  a  microprocessor
  55.         with  a 64K segment size, and it requires special  calls  to
  56.  
  57.  
  58.                                   Page 87
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       Chapter 12 - Dynamic Allocation
  69.  
  70.  
  71.         use data outside of a single segment.  In order to keep  the
  72.         program  small and efficient, these calls are not used,  and
  73.         the  memory  space is limited but still  adequate  for  most
  74.         programs.
  75.  
  76.              A  heap is an area outside of this 64K  boundary  which
  77.         can be accessed by the program to store data and  variables.
  78.         The  data and variables are put on the "heap" by the  system
  79.         as  calls to "malloc" are made.  The system keeps  track  of
  80.         where  the  data  is  stored.  Data  and  variables  can  be
  81.         deallocated  as desired leading to holes in the  heap.   The
  82.         system  knows  where  the holes are and will  use  them  for
  83.         additional  data  storage as more "malloc" calls  are  made.
  84.         The  structure  of  the heap is  therefore  a  very  dynamic
  85.         entity, changing constantly.
  86.  
  87.                             MORE ABOUT SEGMENTS
  88.  
  89.              Most  C  compilers  give the user a  choice  of  memory
  90.         models to use. The user has a choice of using a model with a
  91.         64K limitation for either program or data leading to a small
  92.         fast  program or selecting a 640K limitation  and  requiring
  93.         longer  address calls leading to less efficient  addressing.
  94.         Using  the  larger  address  space  requires  inter  segment
  95.         addressing,  resulting in the slightly slower running  time.
  96.         The  time  is probably insignificant in most  programs,  but
  97.         there are other considerations.
  98.  
  99.              If a program uses no more than 64K bytes for the  total
  100.         of its code and memory and if it doesn't use a stack, it can
  101.         be made into a .COM file.  Since a .COM file is already in a
  102.         memory image format, it can be loaded very quickly whereas a
  103.         file in an .EXE format must have its addresses relocated  as
  104.         it is loaded.  Therefore a tiny memory model can generate  a
  105.         program  that loads faster than one generated with a  larger
  106.         memory model.  Don't let this worry you, it is a fine  point
  107.         that few programmers worry about.
  108.  
  109.              Using  dynamic allocation, it is possible to store  the
  110.         data  on the "heap" and that may be enough to allow  you  to
  111.         use  the small memory model.  Of course, you wouldn't  store
  112.         local  variables such as counters and indexes on  the  heap,
  113.         only very large arrays or structures.
  114.  
  115.              Even  more  important than the need to stay within  the
  116.         small memory model is the need to stay within the  computer.
  117.         If  you  had a program that used several large data  storage
  118.         areas,  but not at the same time,  you could load one  block
  119.         storing  it  dynamically,  then get rid of it and reuse  the
  120.         space for the next large block of data.  Dynamically storing
  121.         each block of data in succession, and using the same storage
  122.  
  123.  
  124.                                   Page 88
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                       Chapter 12 - Dynamic Allocation
  135.  
  136.  
  137.         for  each block may allow you to run your entire program  in
  138.         the computer without breaking it up into smaller programs.
  139.  
  140.                        BACK TO THE "MALLOC" FUNCTION
  141.  
  142.              Hopefully  the above description of the "heap" and  the
  143.         overall plan for dynamic allocation helped you to understand
  144.         what  we are doing with the "malloc"  function.   It  simply
  145.         asks the system for a block of memory of the size specified,
  146.         and  gets  the block with the pointer pointing to the  first
  147.         element of the block.   The only argument in the parentheses
  148.         is the size of the block desired and in our present case, we
  149.         desire  a  block  that will hold one of  the  structures  we
  150.         defined at the beginning of the program.  The "sizeof" is  a
  151.         new  function, new to us at least, that returns the size  in
  152.         bytes of the argument within its parentheses.  It therefore,
  153.         returns the size of the structure named "animal", in  bytes,
  154.         and  that  number is sent to the system  with  the  "malloc"
  155.         call.   At the completion of that call, we have a  block  on
  156.         the heap allocated to us, with "pet1" pointing to the  block
  157.         of data.
  158.  
  159.                               WHAT IS A CAST?
  160.  
  161.              We  still  have  a  funny  looking  construct  at   the
  162.         beginning  of the "malloc" function call.   That is called a
  163.         "cast".   The  "malloc"  function returns a block  with  the
  164.         pointer  pointing  to it being a pointer of type  "char"  by
  165.         default.  Many times, if not most, you do not want a pointer
  166.         to a "char" type variable,  but to some other type.  You can
  167.         define  the  pointer type with the construct  given  on  the
  168.         example line.   In this case we want the pointer to point to
  169.         a  structure of type "animal"